{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4941b17f",
   "metadata": {},
   "source": [
    "# Ideal-gas properties \n",
    "\n",
    "See the appendix of https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=933725 for the mathematical representations. They can be summarized like so:\n",
    "\n",
    "$\n",
    "u_{\\rm ig} = RT\\left((1/T)\\left(\\frac{\\partial\\alpha_{\\rm ig}}{\\partial(1/T)}\\right)_{\\delta}\\right) = RT\\left(\\tau\\left(\\frac{\\partial\\alpha_{\\rm ig}}{\\partial\\tau}\\right)_{\\delta}\\right)\n",
    "$\n",
    "\n",
    "$\n",
    "h_{\\rm ig} = RT\\left(1+(1/T)\\left(\\frac{\\partial\\alpha_{\\rm ig}}{\\partial(1/T)}\\right)_{\\delta}\\right) = RT\\left(1+\\tau\\left(\\frac{\\partial\\alpha_{\\rm ig}}{\\partial\\tau}\\right)_{\\delta}\\right)\n",
    "$\n",
    "\n",
    "$\n",
    "s_{\\rm ig} = R\\left((1/T)\\left(\\frac{\\partial\\alpha_{\\rm ig}}{\\partial(1/T)}\\right)_{\\delta}-\\alpha_{\\rm ig}\\right) = R\\left(\\tau\\left(\\frac{\\partial\\alpha_{\\rm ig}}{\\partial\\tau}\\right)_{\\delta}-\\alpha_{\\rm ig}\\right)\n",
    "$\n",
    "\n",
    "The precise reference state used in Table A-22 of Moran and Shapiro is unknown, but they are based on the gas tables from the 1950s at the then National Bureau of Standards (now NIST). According to [a summary report from one year later](https://www.govinfo.gov/content/pkg/GOVPUB-C13-89baf9f9b4a43e5f25820bd51b0f3f11/pdf/GOVPUB-C13-89baf9f9b4a43e5f25820bd51b0f3f11.pdf), the enthalpy and Gibbs energy (and therefore also the entropy because $g=h-Ts$) are set to 0 at 0 K. This differs to the reference state used in [the pseudo-pure Air EOS](https://coolprop.org/fluid_properties/fluids/Air.html#fluid-air), but the offset is identical, as shown here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73e08117",
   "metadata": {},
   "outputs": [],
   "source": [
    "import CoolProp.CoolProp as CP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3b3722e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Some check values from Moran & Shapiro, 6th edition, Table A-22\n",
    "Ts = [200, 440, 740]\n",
    "hs = [199.97, 441.61, 756.44]\n",
    "ss = [1.29559, 2.08870, 2.63280]\n",
    "us = [142.56, 315.30, 544.02]\n",
    "\n",
    "for T, h0, s0, u0 in zip(Ts, hs, ss, us):\n",
    "    AS = CP.AbstractState(\"HEOS\", \"Air\")\n",
    "    # Use the density obtained from Z=1\n",
    "    rho = 101325/(CP.PropsSI('molemass','Air')*T)\n",
    "    AS.update(CP.DmolarT_INPUTS, rho, T)\n",
    "    R = AS.gas_constant()/AS.molar_mass()\n",
    "    RT = R*T\n",
    "    ucalc_kJkg = RT*(AS.tau()*AS.dalpha0_dTau())/1000 # kJ/kg\n",
    "    hcalc_kJkg = RT*(1+AS.tau()*AS.dalpha0_dTau())/1000 # kJ/kg\n",
    "    scalc_kJkgK = R*(AS.tau()*AS.dalpha0_dTau()-AS.alpha0())/1000 # kJ/kg/K\n",
    "    print('T:', T, 'u:', ucalc_kJkg, 'h:', hcalc_kJkg, 's:', scalc_kJkgK, 'deltau:', ucalc_kJkg-u0, 'deltah:', hcalc_kJkg-h0, 'deltas:', scalc_kJkgK-s0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2990c24c-5c54-4450-a6d1-871d166b1a1c",
   "metadata": {},
   "source": [
    "As of version 7.2, you can also get the ideal-gas properties directly from the model without additional work:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3f6353b-4fae-4ed6-8646-3604aed4d0a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "for T, h0, s0, u0 in zip(Ts, hs, ss, us):\n",
    "    AS = CP.AbstractState(\"HEOS\", \"Air\")\n",
    "\n",
    "    # Use the density obtained from Z=1\n",
    "    rho = 101325/(CP.PropsSI('molemass','Air')*T)\n",
    "    AS.update(CP.DmolarT_INPUTS, rho, T)\n",
    "    R = AS.gas_constant()/AS.molar_mass()\n",
    "    RT = R*T\n",
    "    ucalc_kJkg = AS.umass_idealgas()/1000 # kJ/kg\n",
    "    hcalc_kJkg = AS.hmass_idealgas()/1000 # kJ/kg\n",
    "    scalc_kJkgK = AS.smass_idealgas()/1000 # kJ/kg/K\n",
    "    print('T:', T, 'u:', ucalc_kJkg, 'h:', hcalc_kJkg, 's:', scalc_kJkgK, 'deltau:', ucalc_kJkg-u0, 'deltah:', hcalc_kJkg-h0, 'deltas:', scalc_kJkgK-s0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "456ee5f6-5939-4c02-8036-e2e099fc0065",
   "metadata": {},
   "source": [
    "Or with the high-level interface (less efficient computationally)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "086d4495-69b5-409b-bd5e-355b1356a21e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for T, h0, s0, u0 in zip(Ts, hs, ss, us):\n",
    "    # Use the density obtained from Z=1\n",
    "    rho = 101325/(CP.PropsSI('molemass','Air')*T)\n",
    "    ucalc_kJkg = CP.PropsSI('Umass_idealgas','T|phase_gas',T,'Dmolar',rho,'Air')/1000 # kJ/kg\n",
    "    hcalc_kJkg = CP.PropsSI('Hmass_idealgas','T|phase_gas',T,'Dmolar',rho,'Air')/1000 # kJ/kg\n",
    "    scalc_kJkgK = CP.PropsSI('Smass_idealgas','T|phase_gas',T,'Dmolar',rho,'Air')/1000 # kJ/kg/K\n",
    "    print('T:', T, 'u:', ucalc_kJkg, 'h:', hcalc_kJkg, 's:', scalc_kJkgK, 'deltau:', ucalc_kJkg-u0, 'deltah:', hcalc_kJkg-h0, 'deltas:', scalc_kJkgK-s0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
